package edu.bbu.commonproject.desktop.gui.view.swing.file.lister;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.dnd.DnDConstants;
import java.awt.dnd.DropTarget;
import java.awt.dnd.DropTargetListener;
import java.io.File;
import java.util.TooManyListenersException;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.SwingUtilities;

import edu.bbu.commonproject.desktop.client.services.DirectoryCLTServices;
import edu.bbu.commonproject.desktop.gui.view.swing.file.lister.components.LDirectory;
import edu.bbu.commonproject.desktop.gui.view.swing.file.lister.components.LFile;
import edu.bbu.commonproject.desktop.gui.view.swing.file.lister.components.LParentDirectory;
import edu.bbu.commonproject.desktop.gui.view.swing.file.lister.components.Listable;
import edu.bbu.commonproject.desktop.gui.view.swing.file.lister.utils.WrapLayout;

/**
 * The server file list panel.
 */
public class ServerFileListPanel extends JPanel implements FileLister
{
	/** It initialises the REGULAR_FILE_CONTENT. */
	public static final int REGULAR_FILE_CONTENT = 0;
	
	/**It initialises the VERSION_FILE_CONTENT. */
	public static final int VERSION_FILE_CONTENT = 1;
	
	/** It sets the current directory. */
	private String currentDirectory = DirectoryCLTServices.separator + "base" + DirectoryCLTServices.separator;
	
	/** Document panel. */
	private JPanel documentPanel;
	
	/** The scroll panel.*/
	private JScrollPane scrollPane;
	
	/** The control panel. */
	private ServerFilesControlPanel controlPanel;
	
	/** The selected object. */
	private Listable selectedObject;
	
	/** It initialises the display type. */
	private int displayType = REGULAR_FILE_CONTENT;
	
	/** The drop target. */
	private DropTarget dropTarget;
	/**
	 * The server file lsit panel. Which sets the panels.
	 */
	public ServerFileListPanel()
	{
		dropTarget = new DropTarget();
		dropTarget.setActive(true);
		dropTarget.setComponent(this);
		dropTarget.setDefaultActions(DnDConstants.ACTION_COPY);
		
		documentPanel = new JPanel();
		documentPanel.setBackground(Color.WHITE);
		documentPanel.setLayout(new WrapLayout(WrapLayout.LEFT));
		
		scrollPane = new JScrollPane(documentPanel);
		scrollPane.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_ALWAYS);
		scrollPane.getVerticalScrollBar().setUnitIncrement(35);
		
		controlPanel = new ServerFilesControlPanel();
		
		this.setLayout(new BorderLayout());
		this.setPreferredSize(new Dimension(550, 500));
		
		this.add(scrollPane, BorderLayout.CENTER);
		this.add(controlPanel, BorderLayout.NORTH);
	}
	
	/**
	 * It sets the display type.
	 * @param displayType the display type.
	 */
	public void setDisplayType(int displayType)
	{
		this.displayType = displayType;
	}
	
	/**
	 * It gets the display type.
	 * @return the display type.
	 */
	public int getDisplayType()
	{
		return displayType;
	}
	
	/**
	 * It sets the selected list item.
	 */
	public void setSelectedListItem(Listable selectedObject)
	{
		this.selectedObject = selectedObject;
	}
	
	/**
	 * It gets the selected list item.
	 */
	public Listable getSelectedListItem()
	{
		return this.selectedObject;
	}
	
	/**
	 * It sets the current path.
	 */
	public void setCurrentPath(final String currentDirectory)
	{
		if(currentDirectory.endsWith(DirectoryCLTServices.separator))
		{
			this.currentDirectory = currentDirectory;
		}
		else 
		{
			this.currentDirectory = currentDirectory + DirectoryCLTServices.separator;
		}
		
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				controlPanel.getCurrentDirectoryLabel().setText("Server: " + currentDirectory);
			}
		});
		
	}
	
	/**
	 * It gets the current path.
	 */
	public String getCurrentPath()
	{
		return currentDirectory;
	}
	
	/**
	 * It appends the current path.
	 * @param directory the curent path directory name.
	 */
	public void appendCurrentPath(String directory)
	{
		if(directory.endsWith(DirectoryCLTServices.separator))
		{
			this.currentDirectory = currentDirectory + directory;
		}
		else 
		{
			this.currentDirectory = currentDirectory + directory + DirectoryCLTServices.separator;
		}
		
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				controlPanel.getCurrentDirectoryLabel().setText("Server: " + currentDirectory);
			}
		});
	}
	
	/**
	 * It clears the current path.
	 */
	public void clearCurrentPath()
	{
		this.currentDirectory = DirectoryCLTServices.separator + "base" + DirectoryCLTServices.separator;
		
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				controlPanel.getCurrentDirectoryLabel().setText("Server: " + currentDirectory);
			}
		});
	}
	
	/**
	 * It checks if current path is cleared.
	 * @return the current path state.
	 */
	public boolean isCurrentPathCleared()
	{
		return currentDirectory.equals(DirectoryCLTServices.separator + "base" + DirectoryCLTServices.separator);
	}
		
	/**
	 * It clears the document panel.
	 */
	public void clearDocumentPanel()
	{
		documentPanel.removeAll();
		selectedObject = null;
	}
	
	/**
	 * It refreshes the document panel.
	 */
	public void refreshDocumentPanel()
	{
		documentPanel.revalidate();
		documentPanel.repaint();
	}
	
	/**
	 * It adds the parent directory.
	 * @param parent the parent directory.
	 */
	public void addParentDirectory(LParentDirectory parent)
	{
		documentPanel.add(parent);
		selectedObject = null;
	}
	
	/**
	 * It adds the directory.
	 * @param directory the directory.
	 */
	public void addDirectory(LDirectory directory)
	{
		directory.setFileLister(this);
		documentPanel.add(directory);
		selectedObject = null;
	}
	
	/**
	 * It adds the file.
	 * @param file the file.
	 */
	public void addFile(LFile file)
	{
		file.setFileLister(this);
		documentPanel.add(file);
		selectedObject = null;
	}
	
	/**
	 * The server file control panel.
	 * @return the control panel.
	 */
	public ServerFilesControlPanel getControlPanel()
	{
		return controlPanel;
	}
	
	/**
	 * It adds the drop target listener.
	 * @param dropTargetListener the drop target listener.
	 */
	public void addDropTargetListener(DropTargetListener dropTargetListener)
	{
		try
		{
			dropTarget.addDropTargetListener(dropTargetListener);
		}
		catch (TooManyListenersException e) { }
	}
}
